package cn.ffcs.util.ftp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.multipart.MultiPartRequest;

import com.opensymphony.xwork2.inject.Inject;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.FilePart;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.ParamPart;
import com.oreilly.servlet.multipart.Part;
import org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest;

public class CosMultiPartRequest implements MultiPartRequest
{

    static final Log log = LogFactory.getLog(MultiPartRequest.class);

    private ArrayList<File> alFile;
    private Hashtable files;
    private ArrayList<String> fileNames;
    private ArrayList<String> contentType;
    private ArrayList<String> fileSystemName;
    private Hashtable<String, ArrayList> parameters;
    private Hashtable<String, ArrayList> filePara;
    private ArrayList<String> paraValues;
    private List<String> errors = new ArrayList<String>();

    private long maxSize;

    @Inject(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)
    public void setMaxSize(String maxSize)
    {
        this.maxSize = Long.parseLong(maxSize);
    }

    public String[] getContentType(String arg0)
    {
        ArrayList<String> aList = (ArrayList<String>) files.get(arg0
                + "_ContentType");
        String[] str = new String[aList.size()];
        for (int i = 0; i < aList.size(); i++)
        {
            str[i] = aList.get(i);
        }
        return str;
    }

    public List getErrors()
    {
        return  this.errors;//Collections.EMPTY_LIST;
    }

    public File[] getFile(String arg0)
    {
        ArrayList<File> aList = (ArrayList<File>) files.get(arg0);
        File[] file = new File[aList.size()];
        for (int i = 0; i < aList.size(); i++)
        {
            file[i] = aList.get(i);
        }
        return file;
    }

    public String[] getFileNames(String arg0)
    {
        ArrayList<String> aList = (ArrayList<String>) files.get(arg0 + "_name");
        String[] stt = new String[aList.size()];
        for (int i = 0; i < aList.size(); i++)
        {
            stt[i] = aList.get(i);
        }
        return stt;
    }

    public Enumeration<String> getFileParameterNames()
    {
        return filePara.keys();
    }

    public String[] getFilesystemName(String arg0)
    {
        ArrayList<String> arrayList = (ArrayList<String>) files.get(arg0
                + "_SysName");
        String[] ss = new String[arrayList.size()];
        for (int i = 0; i < arrayList.size(); i++)
        {
            ss[i] = arrayList.get(i);
        }
        return ss;
    }

    public String getParameter(String arg0)
    {
        List<String> v = parameters.get(arg0);
        if (v != null && v.size() == 1)
        {
            return v.get(0);
        }
        return null;
    }

    public Enumeration<String> getParameterNames()
    {
        return parameters.keys();
    }

    public String[] getParameterValues(String arg0)
    {
        List<String> v = parameters.get(arg0);
        if (v != null && v.size() > 0)
        {
            return (String[]) v.toArray(new String[v.size()]);
        }

        return null;
    }

    public void parse(HttpServletRequest request, String saveDir)
            throws IOException
    {
        Part part;

        files = new Hashtable<String, Object>();
        alFile = new ArrayList<File>();
        fileNames = new ArrayList<String>();
        fileSystemName = new ArrayList<String>();
        contentType = new ArrayList<String>();
        filePara = new Hashtable<String, ArrayList>();
        paraValues = new ArrayList<String>();
        parameters = new Hashtable<String, ArrayList>();
            MultipartParser mp = new MultipartParser(request, (int) maxSize);
            mp.setEncoding("utf-8");
            for (int i = 0; (part = mp.readNextPart()) != null; i++)
            {
                if (part.isFile())
                {
                    FilePart fp = (FilePart) part;
                    if (fp.getFileName() != null
                            && !fp.getFileName().equals(""))
                    {
                        alFile = (ArrayList) files.get(fp.getName());
                        if (alFile == null || alFile.size() < 1)
                        {
                            alFile = new ArrayList();
                        }
                        File fil = new File(saveDir + fp.getFileName());
                        fp.writeTo(fil);

                        alFile.add(fil);

                        fileNames = (ArrayList) files.get(fp.getName()
                                + "_name");
                        if (fileNames == null || fileNames.size() < 1)
                        {
                            fileNames = new ArrayList<String>();
                        }
                        fileNames.add(fp.getFileName());

                        fileSystemName = (ArrayList) files.get(fp.getName()
                                + "_SysName");
                        if (fileSystemName == null || fileSystemName.size() < 1)
                        {
                            fileSystemName = new ArrayList<String>();
                        }
                        fileSystemName.add(fp.getFilePath());

                        contentType = (ArrayList) files.get(fp.getName()
                                + "_ContentType");
                        if (contentType == null || contentType.size() < 1)
                        {
                            contentType = new ArrayList<String>();
                        }
                        contentType.add(fp.getContentType());

                        files.put(fp.getName(), alFile);
                        files.put(fp.getName() + "_name", fileNames);
                        files.put(fp.getName() + "_SysName", fileSystemName);
                        files.put(fp.getName() + "_ContentType", contentType);
                        filePara.put(fp.getName(), alFile);
                    }
                }
                else if (part.isParam())
                {
                    ParamPart pp = (ParamPart) part;
                    paraValues = parameters.get(pp.getName());
                    if (paraValues == null || paraValues.size() < 1)
                    {
                        paraValues = new ArrayList<String>();
                    }
                    paraValues.add(pp.getStringValue());
                    parameters.put(pp.getName(), paraValues);

                }
                else
                {

                }
            }
            try
            {
        }
        catch (Exception e)
        {
            e.printStackTrace();
            log.error(e);
            errors.add(e.getMessage());
        }
    }

}
